//Created by Kyle "Fayt" Lowry
//PUSearchForCard.h

#include "PUSearchForCard.h"
#include "input.h"
#include "Controller.h"

PUSearchForCard::PUSearchForCard()
{
	PU_name	= "Search for Artifact Power-Up";
}

int PUSearchForCard::Execute(Input* aInput, Player* aPlayer, Text * aText)
{
	int numCards		= aPlayer->getNumCards();
	newPlayerCards		= new Card*[numCards];
	Card** playerCards	= aPlayer->getCards();

	//Initialize the picked array to false
	for(int i = 0; i < NUM_ARTIFACTS; i++)
	{
		picked[i] = false;
	}

	//Create a random number to properly give each player a set amount of cards
	int num = (rand() % (numCards - 1));

	//Show all of the players cards to the player
	aPlayer->ShowAllCards();

	//Set and draw text
	aText->setText("Press <CTRL> for help.");
	//If either control key is held down
	if( aInput->KeyDown(DIK_LCONTROL) || aInput->KeyDown(DIK_RCONTROL) )
	{
		//Than draw the help text
		aText->setText(	"* All other players should be looking away.\n"
						"* All of the artifacts you need to find are revealed.\n"
						"* Using the mouse click the one you want to currently be searching for.\n" );
	}

	for(int i = 0; i < numCards; i++)
	{
		//If the mouse coordinates are within the card's sprite coordinates
		if(playerCards[i]->GetSpriteInfo()->inSprite(aInput->getMousePosition()))
		{
			//and if the left mouse button is clicked
			if(aInput->leftClicked())
			{
				//Set that card to the card the player is currently searching for
				newPlayerCards[numCards - 1] = playerCards[i];

				//Set the player's card that was just picked equal to the last one in the array
				playerCards[i] = playerCards[numCards - 1];

				for(int j = 0; j < numCards - 1; j++)
				{
					//Pick a random card between 1 and the current number of player cards that has not already been picked
					while(picked[num])
					{
						//Set a new card to be picked if the current card has already been picked
						num = (rand() % (numCards - 1));
					}

					//Set the picked card to true
					picked[num] = true;

					//Shuffle the rest of the cards in the player's deck
					newPlayerCards[j] = playerCards[num];
				}
				//Set the players cards equal to the new array made
				aPlayer->SetCards(newPlayerCards);

				//Hide all of the player's cards
				aPlayer->HideAllCards();

				//The players are able to move after this power-up so return MOVE
				return MOVE;
			}
		}
	}

	//If nothing is clicked just stay in the power-up menu
	return STAY;
}